package com.ruoyi.develop.lvYifahuomingxi.controller;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.constants.BusinessConstants;
import com.ruoyi.develop.goodsSale.domain.GoodsSale;
import com.ruoyi.develop.goodsSale.service.IGoodsSaleService;
import com.ruoyi.develop.lvFahuomingxi.domain.LvFahuomingxi;
import com.ruoyi.develop.lvFahuomingxi.service.ILvFahuomingxiService;
import com.ruoyi.develop.produceRecordDetail.domain.ProduceRecordDetail;
import com.ruoyi.export.service.IExportService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.develop.lvYifahuomingxi.domain.LvYifahuomingxi;
import com.ruoyi.develop.lvYifahuomingxi.service.ILvYifahuomingxiService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 发货单明细Controller
 * @author qh
 * @date 2024-10-08
 */
@RestController
@RequestMapping("/lvYifahuomingxi/lvYifahuomingxi")
public class LvYifahuomingxiController extends BaseController {
    @Autowired
    private ILvYifahuomingxiService lvYifahuomingxiService;
    @Autowired
    private ILvFahuomingxiService lvFahuomingxiService;
    @Autowired
    private IGoodsSaleService goodsSaleService;
    @Autowired
    private IExportService exportService;


    /**
     * 查询发货单明细列表
     */
    @PreAuthorize("@ss.hasPermi('develop:LvYifahuomingxi:query')")
    @GetMapping("/list")
    public TableDataInfo list(LvYifahuomingxi entity) {
        startPage();
        List<LvYifahuomingxi> list = lvYifahuomingxiService.selectList(entity);
        return getDataTable(list);
    }

    /**
     * 查询发货单明细列表  同类产品合并
     */
    @PreAuthorize("@ss.hasPermi('develop:LvYifahuomingxi:query')")
    @GetMapping("/listMerge")
    public TableDataInfo listMerge(LvYifahuomingxi entity) {
        startPage();
        List<LvYifahuomingxi> list = lvYifahuomingxiService.selectListMerge(entity);
        return getDataTable(list);
    }



    /**
     * 发货记录模块：不看未发货的和弃审的
     */
    @GetMapping("/listNoCommit")
    public TableDataInfo listNoCommit(LvYifahuomingxi entity) {
        startPage();
        List<LvYifahuomingxi> list = lvYifahuomingxiService.listNoCommit(entity);
        return getDataTable(list);
    }
    /**
     * 修改发货记录：查询未发货的、弃审的、新选择准备发货的
     */
    @GetMapping("/listNoFahuo")
    public TableDataInfo listNoFahuo(LvYifahuomingxi entity) {
        startPage();
        List<LvYifahuomingxi> list = lvYifahuomingxiService.listNoFahuo(entity);
        return getDataTable(list);
    }

    /**
     * 发货产品明细列表导出
     */
    @Log(title = "发货产品明细列表导出", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(LvYifahuomingxi entity, HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> params = new HashMap<>();
        String date = DateUtils.getTime();//默认当天
        startPage();
        List<LvYifahuomingxi> list = lvYifahuomingxiService.selectList(entity);
        list.stream().forEach(e->{
            Map<String, Object> map = new HashMap<String, Object>();
//            map.put("fundCallback", DictUtils.getDictLabel(BusinessConstants.FUND_CALLBACK, e.getFundCallback()));
            map.put("fahuoDate", DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, e.getFahuoDate()));
            e.setParams(map);
        });
        params.put("list", list);
        params.put("date", date);
        String fileName = request.getParameter("fileName");
        if(StringUtils.isBlank(fileName)){
            fileName = "发货产品信息.xlsx";
        }
        String templateName = "发货产品信息.xlsx";
        exportService.baseXlsxExport(fileName, params, templateName, request, response);
        return null;
    }

    /**
     * 获取发货单明细详细信息
     */
    @PreAuthorize("@ss.hasPermi('develop:LvYifahuomingxi:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return AjaxResult.success(lvYifahuomingxiService.selectById(id));
    }

    /**
     * 新增发货单明细
     */
    @PreAuthorize("@ss.hasPermi('develop:LvYifahuomingxi:add')")
    @Log(title = "发货单明细", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody LvYifahuomingxi entity) {
        return toAjax(lvYifahuomingxiService.insert(entity));
    }

    @PostMapping("/insertForFh")
    public AjaxResult insertForFh(@Validated @RequestBody LvYifahuomingxi entity) {
        lvYifahuomingxiService.insertBatch(entity);
        return AjaxResult.success("选择完成！");
    }
    //弃审重新申请（删除原有的兵新增现在的）
    @PostMapping("/updateForFh")
    public AjaxResult updateForFh(@Validated @RequestBody LvYifahuomingxi entity) {
        //lvYifahuomingxiService.deleteByFahuoId(entity.getFahuoId());
        lvYifahuomingxiService.updateForFh(entity);
        return AjaxResult.success("选择完成！");
    }
    /**
     * 修改发货单明细
     */
    @PreAuthorize("@ss.hasPermi('develop:LvYifahuomingxi:edit')")
    @Log(title = "发货单明细", businessType = BusinessType.UPDATE)
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody LvYifahuomingxi entity) {
        return toAjax(lvYifahuomingxiService.update(entity));
    }

    /**
     * 删除发货单明细
     */
    @PreAuthorize("@ss.hasPermi('develop:LvYifahuomingxi:remove')")
    @Log(title = "发货单明细", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(lvYifahuomingxiService.deleteByIds(ids));
    }


    @DeleteMapping("/removeFh/{id}")
    public AjaxResult removeFh(@PathVariable String[] id) {
        LvYifahuomingxi lvYiFahuomingxi=lvYifahuomingxiService.selectById(id[0]);
        LvFahuomingxi lvFahuomingxi=lvFahuomingxiService.selectById(lvYiFahuomingxi.getTongzhiId());
        lvFahuomingxi.setShengyuCount(lvFahuomingxi.getShengyuCount().add(lvYiFahuomingxi.getShuliang()));
        lvFahuomingxi.setFahuoCount(lvFahuomingxi.getFahuoCount().subtract(lvYiFahuomingxi.getShuliang()));
        lvFahuomingxi.setStatus(BusinessConstants.FAHUO_0);
        lvFahuomingxi.setIsFahuo(BusinessConstants.FAHUO_1);
        lvFahuomingxiService.update(lvFahuomingxi);
        return toAjax(lvYifahuomingxiService.deleteByIds(id));
    }



    @DeleteMapping("/removeSendDetail/{id}")
    public AjaxResult removeSendDetail(@PathVariable String[] id) {
        for (String detailId : id) {
            lvYifahuomingxiService.deleteByIds(id);
            LvYifahuomingxi lvYiFahuomingxi=lvYifahuomingxiService.selectById(detailId);
            GoodsSale goodsSale = goodsSaleService.selectById(lvYiFahuomingxi.getSaleId());
            goodsSale.setFahuoShengyu(goodsSale.getFahuoShengyu().add(lvYiFahuomingxi.getShuliang()));
            goodsSale.setFahuoCount(goodsSale.getFahuoCount().subtract(lvYiFahuomingxi.getShuliang()));
            goodsSaleService.update(goodsSale);
        }
        return toAjax(1);
    }




    /** * 从 订单产品中选取产品 发货  */
    @PostMapping("/insertSendDetailFormGoodsSale")
    public AjaxResult insertSendDetailFormGoodsSale(@Validated @RequestBody LvYifahuomingxi entity) {
        lvYifahuomingxiService.insertSendDetailFormGoodsSale(entity);
        return AjaxResult.success("选择完成！");
    }







}
